package com.ActiveMQ;

import com.mapper.NewsMapper;
import com.model.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;

/**
 * Created by zhenzhang
 * Date 2017/12/21
 * Desc 生产者rest .
 */
@Service
public class CustomerRest {

    @Autowired
    private NewsMapper newsMapper;
    /**
     * 监听方
     */
    //监听注解
    @JmsListener(destination = "beyondLiQueueTest")
    public void getQueue(String info) {
        System.out.println("成功监听beyondLiQueue消息队列，传来的值为:" + info);
    }

    //监听注解news
    @JmsListener(destination = "top")
    public void getDelayQueue(String info) {
        System.out.println("成功监听beyondLiQueueTest消息队列，传来的值为:" + info + "当前时间为" + LocalDateTime.now());

        News news = new News();
        String[] split = info.split("\\|");
        for (int i = 0; i < split.length; i++) {
            news.setId(UUID.randomUUID().toString().replace("-",""));
            news.setStatus("0");
            news.setTime(new Date());
            news.setCategoryId(split[2]);
            news.setTitle(split[0]);
            news.setLink(split[1]);
            if(split.length==4){
                //读取图片
              /*      String name="";
                    InputStream stream = new FileInputStream(new File("D:/photo/"+split[3]));
                    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[2048];
                    int len = -1;
                    while((len = stream.read(buffer)) != -1){
                        outStream.write(buffer, 0, len);
                    }
                    outStream.close();
                    stream.close();
                    byte[] byt = outStream.toByteArray();
                    String suffix = FilenameUtils.getExtension(name);
                    String[] str = FastdfsUtil.upload(byt,suffix);
                    String name1=str[0]+"/"+str[1];*/

                news.setPhoto(split[3]);
            }else{
                news.setPhoto("");
            }
        }
        newsMapper.insert1(news);

    }
}